'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: 5_json_out_put.py
* @Time: 2025/7/10
* @All Rights Reserve By Brtc
'''
from os.path import ALLOW_MISSING

import dotenv
from langchain_community.chat_models import ChatOpenAI
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.prompts import ChatPromptTemplate
from pydantic import BaseModel, Field

dotenv.load_dotenv()

# 构建一个 Jocker 类
class Jocker(BaseModel):
    joker_name : str = Field(description = "回答用户冷笑话")
    punch_line: str = Field(description="这个冷笑话的笑点")

#构建一个Json 输出解析器
parser = JsonOutputParser(pydantic_object=Jocker)

# 构建大模型
llm = ChatOpenAI(model = "gpt-3.5-turbo-16k")

#构建提示词通过数据结构获取fomat_instructions的提示词并进行填充
prompt = (ChatPromptTemplate.from_template("请根据用户的提问进行回答。\n {fomat_instructions}\n{query}")
          .partial(fomat_instructions=parser.get_format_instructions()))
print(prompt)

# 模型输出
joke = parser.invoke(llm.invoke(prompt.invoke({"query":"请讲一个关于程序员的冷笑话"})))
print(joke)
